﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    /// <summary>
    /// Class to be used in building code
    /// </summary>
    public class CodeProvider
    {
        public static readonly string CommaAndNewLine = string.Concat(",", System.Environment.NewLine);
        public static readonly string CommaAndSpace = ", ";

        public CodeProvider()
        {
            Builder = new StringBuilder();
        }

        public StringBuilder Builder { get; private set; }

        public int Indentantion { get; set; }

        #region Overridable

        public override string ToString()
        {
            string code = Builder.ToString();
            Builder.Clear();

            return code;
        }

        public virtual void BeginBlock()
        {
            ++Indentantion;
        }

        public virtual void EndBlock()
        {
            --Indentantion;
        }

        #endregion

        #region Fluent methods

        public CodeProvider Indent()
        {
            Builder.Append(new string(' ', Indentantion * 4));
            return this;
        }

        public CodeProvider Append(string s)
        {
            Builder.Append(s);
            return this;
        }

        public CodeProvider AppendLine(string s = null)
        {
            Builder.AppendLine(s);
            return this;
        }

        #endregion
    }
}
